﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace EtlComm
{
    public class UpdateData
    {
        public string StationID { get; set; }
        public string TaskID { get; set; }
        public string BeginParam { get; set; }
        public string EndParam { get; set; }
        public string ExeTime { get; set; }

        public override string ToString()
        {
            return string.Format("{0}|{1}|{2}|{3}|{4}|{5}","SJW", StationID, TaskID, BeginParam, EndParam,ExeTime);
        }
    }

    /// <summary>
    /// 为读写定义数据协议
    /// </summary>
    public class EtlStreamString
    {
        private Stream ioStream;
        private UnicodeEncoding streamEncoding;

        public EtlStreamString(Stream ioStream)
        {
            this.ioStream = ioStream;
            streamEncoding = new UnicodeEncoding();
        }

        /// <summary>
        /// 读取字符串
        /// </summary>
        /// <returns>字符串</returns>
        public string ReadString()
        {
            int len = 0;
            len = ioStream.ReadByte() * 256;
            len += ioStream.ReadByte();
            byte[] inBuffer = new byte[len];
            ioStream.Read(inBuffer, 0, len);

            return streamEncoding.GetString(inBuffer);
        }
        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="outString">字符串</param>
        /// <returns>写入字节</returns>
        public int WriteString(string outString)
        {
            byte[] outBuffer = streamEncoding.GetBytes(outString);
            int len = outBuffer.Length;
            if (len > UInt16.MaxValue)
            {
                len = (int)UInt16.MaxValue;
            }
            ioStream.WriteByte((byte)(len / 256));
            ioStream.WriteByte((byte)(len & 255));
            ioStream.Write(outBuffer, 0, len);
            ioStream.Flush();

            return outBuffer.Length + 2;
        }
    }
}
